package com.henry.db.migrate.model;

import com.henry.db.migrate.util.RandomValueUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

public class TableModel {
    static final int batchSize;
    static List<String> colTypes;
    static {
        batchSize=3000;
        colTypes = new ArrayList<>();
        colTypes.add(" CHAR(36)");
        colTypes.add(" INTEGER");
        colTypes.add(" VARCHAR(255)");
        colTypes.add(" DATETIME");
        colTypes.add(" DECIMAL(18,3)");
    }

    public final int num;

    private String tablename;
    private String createSql;
    private String dropSql;
    private String insertSql;

    public TableModel(int num) {
        this.num = num;
        this.tablename = "mgr_table_" + num;
        this.createSql = genCreateSql();
        this.dropSql = genDropSql();
        this.insertSql = genInsertSql();

    }

    private String genDropSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("drop table if exists ");
        sb.append(this.tablename);
        return sb.toString();
    }

    private String genCreateSql() {
        StringBuilder sb = new StringBuilder();
        sb.append("create table if not exists ");
        sb.append(this.tablename);
        sb.append("(\r\n");
        for (int j = 1; j <= num + 5; j++) {
            sb.append("t" + num + "_col" + j);
            sb.append(colTypes.get((j - 1) % 5));
            sb.append(",\r\n");
        }
//		sb.deleteCharAt(sb.length()-1);
        sb.append("primary key(t" + num + "_col1)");
        sb.append(")");
        return sb.toString();
    }

    private String genInsertSql() {
//        insert into user(uuid, name, sex, address, phone,col1,col2,col3,col4,col5) values (?,?,?,?,?,?,?,?,?,?)
        StringBuilder sb = new StringBuilder();
        sb.append("insert into ");
        sb.append(this.tablename);
        sb.append("(");
        for (int j = 1; j <= num + 5; j++) {
            sb.append("t" + num + "_col" + j);
            sb.append(",");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(")values(");
        for (int j = 1; j <= num + 5; j++) {
            sb.append("?,");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(")");
        return sb.toString();
    }

    public Object[][] genParams() {
//        String[] colnames = baseTable.getColnames();
        Object[][] params = new Object[batchSize][this.num+5];
        for (int i = 0; i < batchSize; i++) {
            for (int j = 1; j <= this.num+5; j++) {
                params[i][j - 1] = getColumnVal(j);
            }
        }
        return params;
    }

    private static Object getColumnVal(int j){
        Random random = new Random();
        DecimalFormat df=new DecimalFormat("#.000");
        if(j%5==1)
            return UUID.randomUUID().toString();
        else if(j%5==2)
            return random.nextInt(65535);
        else if(j%5==3)
            return RandomValueUtil.getChineseName()+RandomValueUtil.getEmail(8,32);
        else if(j%5==4)
            return new Date();
        else
            return  new BigDecimal(df.format(random.nextDouble()*1000000000000000L));
    }

    public String getTablename() {
        return tablename;
    }

    public String getCreateSql() {
        return createSql;
    }

    public String getDropSql() {
        return dropSql;
    }

    public String getInsertSql() {
        return insertSql;
    }

    public static void main(String[] args) {
        TableModel tableModel = new TableModel(1);
        System.out.println(tableModel.getInsertSql());
    }
}
